<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8" />
  <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <title>Document</title>
  <style>
    .area {
      width: 600px;
      height: 400px;
      background-color: skyblue;
      color: #fff;
      line-height: 400px;
      text-align: center;
      font-size: 40px;
      margin: 40px auto;
    }
  </style>
</head>

<body>
  <div class="area" id="normal">正常</div>
  <div class="area" id="debounce">防抖</div>
  <div class="area" id="throttle">节流</div>
  <script src="https://cdn.bootcdn.net/ajax/libs/lodash.js/4.17.21/lodash.js"></script>
  <script>
    const normal = document.querySelector("#normal")
    const debounce = document.querySelector("#debounce")
    const throttle = document.querySelector("#throttle")
    let normalCount = 0
    normal.onmousemove = function () {
      normal.innerText = `正常移动了${normalCount}次`
      normalCount++
    }
    let debounceCount = 0
    let debounceTimer = null

    function addCount() {
      debounce.innerText = `防抖移动了${debounceCount}次`
      debounceCount++
    }
    debounce.onmousemove = _.debounce(addCount, 500)
    //   debounce.onmousemove = function () {
    //     //   如果一个事件在单位事件内频繁触发 就 只执行最后一次
    //     clearTimeout(debounceTimer) // 清空上一次的定时器 清掉了500 之内还没执行的逻辑
    //     debounceTimer = setTimeout(() => {
    //       debounce.innerText = `防抖移动了${debounceCount}次`
    //       debounceCount++
    //     }, 500)
    //   }
    let throttleCount = 0
    let throttleTimer = null

    function addCount2() {
      throttle.innerText = `节流移动了${throttleCount}次`
      throttleCount++
    }
    throttle.onmousemove = _.throttle(addCount2, 300)
    //   throttle.onmousemove = function () {
    //     if (!throttleTimer) {
    //       // 节流单位事件内执行一次
    //       throttleTimer = setTimeout(() => {
    //         throttle.innerText = `节流移动了${throttleCount}次`
    //         throttleCount++
    //         throttleTimer = null
    //       }, 500)
    //     }
    //   }
  </script>
</body>

</html>